using System;
using System.Net;
using System.Reflection;
using System.Collections;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Gtk;

namespace Whitemice.Consonance
{	
	public partial class MainWindow
	{
		protected void SetupHandlers()
		{
			contactList.OnContactEdit = new EntityEventHandler(PresentCallback);
			contactList.OnContactRemove = new EntityEventHandler(RemoveCallback);
			enterpriseList.OnEnterpriseEdit = new EntityEventHandler(PresentCallback);
			enterpriseList.OnEnterpriseRemove = new EntityEventHandler(RemoveCallback);
			taskLists.OnTaskAccept += new EntityEventHandler(PresentCallback);
			taskLists.OnTaskActivate += new EntityEventHandler(PresentCallback);
			taskLists.OnTaskArchive += new EntityEventHandler(PresentCallback);
			taskLists.OnTaskComment += new EntityEventHandler(PresentCallback);
			taskLists.OnTaskDone += new EntityEventHandler(PresentCallback);
			taskLists.OnTaskEdit += new EntityEventHandler(PresentCallback);
			taskLists.OnTaskReject += new EntityEventHandler(PresentCallback);
			projectList.OnProjectEdit += new EntityEventHandler(PresentCallback);
			ZOGIApplication.Server.OnException += 
				new zOGIExceptionHandler(OnServerException);
			ZOGIApplication.Server.OnUserMessage += 
				new zOGIUserMessageHandler(OnUserMessage);
			ZOGIApplication.Server.OnReceivedPluginData += 
				new zOGIPluginDataHandler(OnReceivedPluginData);
		}
				
		/* UI requested deletion of an entity */
		public virtual void RemoveCallback(
			Entity _entity,
			object _context,
			Exception _exception)
		{
			if (Consonance.RaiseWindow(_entity))
			{
				/* Object is being viewed, present the window and notify user */
				new MessageBox("Object#" + _entity.ObjectId.ToString() + " is currently in use.");
			} else
				{
					if (_entity.CanModify)
					{
						/* User has modification privileges */
						if (Server.Remove(_entity))
						{
							/* Delete was successful */
							Consonance.UnregisterWindow(_entity, true);
							new MessageBox("Deletion successful.");
						} else 
							{
								/* Delete failed */
								new MessageBox("Deletion failed");
							}
					} else 
						{
							/* Insufficient privileges to delete object */
							new MessageBox("You lack sufficient privilages to perform the requested action");
						}
				}
		}
		
		/* UI requested an entity be presented */
		public virtual void PresentCallback(
			Entity _entity,
			object _context,
			Exception _exception)
		{
			string	context;
			
			if (_exception != null)
			{
				new MessageBox(_exception);
				return;
			}
			if (_entity != null)
			{
				if (_context is string)
					context = _context as string;
					else return;
				switch (_entity.EntityName)
				{
					case "Contact":
						if (!(Consonance.RaiseWindow(_entity)))
							new ContactWindow((_entity as Contact));
						break;
					case "Enterprise":
						if (!(Consonance.RaiseWindow(_entity)))
							new EnterpriseWindow((_entity as Enterprise));
						break;					
					case "Task":
						switch(context)
						{
							case "edit":
								if (!(Consonance.RaiseWindow(_entity)))
									new TaskWindow((_entity as Task));
								break;
							case "accept":
								new TaskActionDialog(
									this,
									(_entity as Task),
									TaskAction.Accept);
								break;
							case "comment":
								new TaskActionDialog(
									this,
									(_entity as Task),
									TaskAction.Comment);							
								break;
							case "reject":
								new TaskActionDialog(
									this,
									(_entity as Task),
									TaskAction.Reject);							
								break;
							case "done":
								new TaskActionDialog(
									this,
									(_entity as Task),
									TaskAction.Done);							
								break;
							case "archive":
								new TaskActionDialog(
									this,
									(_entity as Task),
									TaskAction.Archive);							
								break;
							case "activate":
								new TaskActionDialog(
									this,
									(_entity as Task),
									TaskAction.Activate);							
								break;								
						}
						break;
					case "Project":
						if (!(Consonance.RaiseWindow(_entity)))
							new ProjectWindow((_entity as Project));
						break;											
				} /* End entity type switch */
			} /* End if _entity is not null */
		} /* End PresentCallback */
		
		protected virtual void OnQuickIdRequestEvent(
			object _sender, 
			System.EventArgs _e)
		{
			SingleResultCallback callback;
			int		objectId;
			
			/* We know any entry less than 5 characters cannot be
			   a valid objectId since OGo starts allocating 
			   objectIds at 10,000 */
			if (quickId.Text.Length < 5) 
				return;
			objectId = 0;
			try { objectId = Convert.ToInt32(quickId.Text); }
				catch (Exception exception) 
				{
					OnServerException(exception, 0, "User error");
				}
			/* Id was not a possible object id; bail out */
			if (objectId == 0) 
				return;
			/* Setup and fire the callback */
			callback = new SingleResultCallback(PresentCallback);
			Consonance.Server.Get(objectId, callback, "edit", 65535);
		} /* End OnQuickIdRequestEvent */

		protected virtual void OnContactsCRUDActivated (
			object _sender, 
			System.EventArgs _e)
		{
			new ContactCRUD();
		} /* End OnContactsCRUDActivated */

		protected virtual void OnDialogInformationActivated (
			object sender, 
			System.EventArgs e)
		{
		}

		protected virtual void OnPerformanceActivated (
			object sender, 
			System.EventArgs e)
		{
			performanceDialog.Visible = true;
			performanceDialog.Present();
		}

	  protected virtual void OnAboutActionActivated (object sender, System.EventArgs e)
		{
		}
	}
}